import { getCartInfo } from '@api'
export default {
  namespaced: true,
  state: {
    // 购物车购买商品
    items: [],
    total: 0 // 总价格
  },
  mutations: {
    init_cart_info (state, { items, total }) {
      // 购物车数据的初始化
      state.items = items
      state.total = total
    }
  },
  actions: {
    get_cart ({ commit }) {
      // 请求购物车数据
      getCartInfo({
        token: localStorage.getItem('token')
      }).then(res => {
        if (res.data.code === 0) {
          commit('init_cart_info', {
            items: res.data.data.items,
            total: res.data.data.price
          })
        }
      })
    }
  }
}
